package com.qn.gateway.fingerprint;

import io.netty.handler.codec.http.HttpHeaders;
import org.owasp.netryx.constant.HandleCode;
import org.owasp.netryx.intrusion.DetectionResult;
import org.owasp.netryx.intrusion.IntrusionDetector;
import org.owasp.netryx.mitigation.intrusion.constant.DetectCode;
import org.owasp.netryx.mitigation.intrusion.model.IntrusionDetectionData;
import reactor.core.publisher.Mono;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
public class FingerprintIntrusion implements IntrusionDetector {
    @Override
    public Mono<DetectionResult> detect(IntrusionDetectionData data) {

        HttpHeaders httpHeaders = data.getRequest().headers();
        if (data.getJa4Fingerprint() != null) {
            httpHeaders.set("X-JA4-Fingerprint", data.getJa4Fingerprint().getHash());
        }
        if (data.getHttp2Fingerprint() != null) {
            httpHeaders.set("X-HTTP2-Fingerprint", data.getHttp2Fingerprint().toString());
        }
        if (data.getJa4HttpFingerprint() != null) {
            httpHeaders.set("X-JA4H-Fingerprint", data.getJa4HttpFingerprint().getValue());
        }
        return Mono.just(new DetectionResult(DetectCode.OK, data, "User is OK"));
    }
    @Override
    public Mono<HandleCode> onDetected(DetectionResult result) {
        return Mono.just(HandleCode.BLOCK);
    }
    @Override
    public String name() {
        return "fingerprint-intrusion-handler";
    }
}